package com.mew.user.controller;

import com.mew.common.dc.db.LoginoutService;
import com.mew.common.enm.ExitType;
import com.mew.common.rest.sas.ExitGameSasRequest;
import com.mew.common.rest.sas.LogoutSasRequest;
import com.mew.provide.SasFeignService;
import com.zebra.common.event.EventBusAsyncCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Date 2020/10/21 0021
 **/
@RestController
@Slf4j
public class LoginExiteController {

    @Autowired
    private LoginoutService loginoutService;

    @GetMapping(value = "/exit/{uid}/{exitType}")
    @ResponseBody
    public Integer exitLobby(@PathVariable("uid") long uid, @PathVariable("exitType") int exitType){
        if (uid < 1){
            return 0;
        }
        log.info("Lobby Exit: {} {}", uid, exitType);
        try {
            loginoutService.logout(uid,  exitType);
            EventBusAsyncCenter.post(LogoutSasRequest.builder().userId(uid).build()); //TODO 退出大厅
        }catch (Exception e){
            log.error("uid: "+uid, e);
            return 0;
        }
        return 1;
    }

    /**
     * clear game
     */
    @GetMapping(value = "/clear/{uid}")
    @ResponseBody
    public Integer exitGame(@PathVariable("uid") long uid){
        log.info("Lobby exitGame: {} {}", uid);
        if (uid < 1){
            return 0;
        }
        try {
            loginoutService.exitRoom(uid, ExitType.Repeat.code());
            EventBusAsyncCenter.post(ExitGameSasRequest.builder().userId(uid).build());  //TODO 退出游戏
        }catch (Exception e){
            log.error("uid: "+uid, e);
            return 0;
        }
        return 1;
    }





}
